package cc.cc4414.cc02.system.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import cc.cc4414.cc02.system.entity.User;
import java.lang.Integer;

/**
 * @author cc 2018年6月12日
 */
public interface UserRepository extends JpaRepository<User, Long> {
	User findByUsername(String username);

	User findByUsernameAndState(String username, Integer state);

	Page<User> findByState(Integer state, Pageable pageable);

	/**
	 * 删除id为userId的用户的所有角色
	 * 
	 * @param userId
	 *            用户id
	 */
	@Modifying
	@Query(value = "delete from user_role where user_id = ?", nativeQuery = true)
	void deleteUserRoleByUserId(Long userId);

	/**
	 * 为用户userId赋予角色roleId
	 * 
	 * @param userId
	 *            用户id
	 * @param roleId
	 *            角色id
	 */
	@Modifying
	@Query(value = "insert into user_role (user_id, role_id) values (?1, ?2)", nativeQuery = true)
	void saveUserRoleByUserIdAndRoleId(Long userId, Long roleId);
}
